package com.example.ymm.util;

import lombok.extern.slf4j.Slf4j;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Slf4j
public class EmailUtil {

    private static String host = "smtp.qq.com";
    private static String port = "587";
    private static String username = "1055162826@qq.com";
    private static String password = "inxdqfdcopldbfha";

    public static void sendEmail(String toAddress, String subject, String message) {
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");

        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        };

        Session session = Session.getInstance(properties, authenticator);

        try {
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(username));
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
            mimeMessage.setSubject(subject);
            mimeMessage.setText(message);

            Transport.send(mimeMessage);

            log.info("邮箱发送成功，主题："+subject+"  内容："+message);
        } catch (MessagingException e) {
            log.error("Failed to send email. Error: " + e.getMessage());
        }
    }


    public static void sendOutProfit(String message){
        sendEmail(username,"出库利润跟踪",message);
    }

//    public static void main(String[] args) {
//        sendEmail(username,"出库利润跟踪","订单号:235235\n利润为:235345\n入库总额:3456\n出库总额:456");
//    }

}